package edu.rpi.tw.escience.stargazer.math;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.Repository;
import edu.rpi.tw.escience.stargazer.server.StarGazerContext;
import edu.rpi.tw.escience.stargazer.data.Location;
import edu.rpi.tw.escience.stargazer.data.Position;
import edu.rpi.tw.escience.stargazer.data.SkyRegion;
import edu.rpi.tw.escience.stargazer.data.Time;
import edu.rpi.tw.escience.stargazer.onto.Star;

public class Math {

        public static final double JD_J2000 = 2451545.0;  // Julian date for the J2000.0 epoch.

        public static Collection<Star> getStarsWithinLocation(Repository rep, Location l, Time t, SkyRegion r) throws RepositoryException{
                /**
                 * @description Given a Location and Time it will return a collection of stars 
                 * @param l is the Location Object
                 * @param t is the Time object
                 * @param r Region of Sky
                 * @return List of stars that belongs to 
                 */
                StarGazerContext sgc = new StarGazerContext(rep);
                Collection<Star> all = sgc.getAllStars();
                Collection<Star> results = new LinkedList<Star>();
                Iterator<Star> it = all.iterator();

				int i = 0;
                while(it.hasNext()){
                        Star s = it.next();
						
                        String ra = s.getHasRightAscension(); //Get Right Ascension
                        String d = s.getHasDeclination(); //Get Declination

                        Position p = new Position(ra, d);
                        Position p2 = p.ephemeris(t);
                        //Do something with location that I don't know!!!!

                        if(r.isPositionInRegion(p2)){
								results.add(s);
                        }
                }
                return results;
        }

        public static Star getStarDetailsByName(Repository rep, String name) throws RepositoryException{
                /*
                 * @description returns star object that matches the name
                 * @param name a String with star name
                 * @returns a star object that matches the name
                 */
                StarGazerContext sgc = new StarGazerContext(rep);
                Collection<Star> all = sgc.getAllStars();
                Collection<Star> results = new LinkedList<Star>();
                Iterator<Star> it = all.iterator();
                while(it.hasNext()){
                        Star s = it.next();
                        if(s.getHasName().compareTo(name) == 0){
                                return s;
                        }

                }        
                //not found
                return null;
        }
        
        public static Star getStarDetailsByNGC(String NGC) throws RepositoryException{
                /*
                 * @description returns star object that matches NGC number
                 * @param NGC a String with NGC number;
                 * @returns a star object that matches NGC number
                 */
                StarGazerContext sgc = new StarGazerContext(null);
                Collection<Star> all = sgc.getAllStars();
                Collection<Star> results = new LinkedList<Star>();
                Iterator<Star> it = all.iterator();
                while(it.hasNext()){
                        Star s = it.next();
                        if(s.getHasNGCNumber().compareTo(NGC) == 0){
                                return s;
                        }

                }        
                //not found
                return null;
        }
}
